package com.example.user.customviewdemo.camera;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Button;
import android.widget.LinearLayout;

import com.example.user.customviewdemo.R;

import butterknife.Bind;
import butterknife.ButterKnife;

public class CameraActivity extends AppCompatActivity implements SurfaceHolder.Callback {

    @Bind(R.id.start_button)
    Button startButton;
    @Bind(R.id.stop_button)
    Button stopButton;
    @Bind(R.id.container)
    LinearLayout container;
 /*   @Bind(R.id.surface_view)
    SurfaceView surfaceView;*/

    private SurfaceView surfaceView;
    private SurfaceHolder holder;
    private RecordThread thread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        ButterKnife.bind(this);

      /*  WindowManager.LayoutParams param = new WindowManager.LayoutParams();
        param.type=WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;     // 系统提示类型,重要
        param.format=1;
        param.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; // 不能抢占聚焦点
        param.flags = param.flags | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
        param.flags = param.flags | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS; // 排版不受限制

        param.alpha = 1.0f;

        param.gravity= Gravity.LEFT|Gravity.TOP;   //调整悬浮窗口至左上角
        //以屏幕左上角为原点，设置x、y初始值
        param.x=0;
        param.y=0;

        //设置悬浮窗口长宽数据
        param.width=140;
        param.height=140;


        View v = LayoutInflater.from(this).inflate(R.layout.camera_surface_layout,null);
        surfaceView = (SurfaceView) v.findViewById(R.id.surface_view);
        WindowManager manager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        manager.addView(v,param);


        holder = surfaceView.getHolder();
        holder.addCallback(this); // holder加入回调接口
        // 设置setType
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);


        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                thread = new RecordThread(20000, surfaceView, holder);
                thread.start();

            }
        });
        stopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CameraActivity.this, BouncingBallsActivity.class);
                startActivity(intent);
            }
        });*/
        Log.v("tag","开始录制");
        Intent intent = new Intent(this,CamreaService.class);
        startService(intent);
    }

    @Override
    protected void onPause() {
        super.onPause();
       /* if (thread != null) {
            thread.stopRecord();
        }*/

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        this.holder = holder;

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        this.holder = holder;
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        holder = null;
        this.surfaceView = null;
    }
}
